import Vue from 'vue'
/*request:请求方法；callback:列表的请求方法；params：删除接口的参数对象；message：删除成功后的提示信息，不传默认为‘删除成功’*/
let _this = new Vue()
Vue.prototype.delList = function(request, callback, params, message) {
    if (params.ids.length) {
        _this.$confirm({
            title: '确定是否删除数据？',
            okText: '是',
            okType: 'danger',
            cancelText: '否',
            onOk() {
                request(params).then(res => {
                    let msg = message || '删除成功'
                    if (res.data.code === 200) {
                        if (callback) {
                            callback()
                        }
                        _this.$message.success(msg)
                    }
                })
            },
            onCancel() {
            }
        })
    } else {
        _this.$message.warning('请选择要删除的数据')
    }
}

/*request:请求方法；callback:列表的请求方法;params：作废参数对象*/
Vue.prototype.invalidList = function(request, callback, params) {
    _this.$confirm({
        title: '确定作废此数据吗？',
        okText: '是',
        okType: 'danger',
        cancelText: '否',
        onOk() {
            request(params).then(res => {
                if (res.data.code === 200) {
                    if (callback) {
                        callback()
                    }
                    _this.$message.success('作废成功')
                } else {
                    _this.$message.error(res.data.message)
                }
            })
        },
        onCancel() {
        }
    })
}

//判断日期大小
Vue.prototype.compareSize = function(start, end, message) {
    let _end = new Date(end).getTime()
    let _start = new Date(start).getTime()
    if (_start && _end) {
        if (_start > _end) {
            _this.$message.error(message)
            return true
        }
    }
}


// 获取当前时间
Vue.prototype.getNowTime = function(val, type) {
    let date = val ? new Date(val) : new Date()
    let month = date.getMonth() + 1
    let strDate = date.getDate()
    // console.log(date);
    if (month >= 1 && month <= 9) {
        month = '0' + month
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
    }
    let currentDate = ''
    switch (type) {
        case 'Y':
            currentDate = date.getFullYear()
            break
        case 'M':
            currentDate = date.getFullYear() + '-' + month
            break
        case 'D':
            currentDate = date.getFullYear() + '-' + month + '-' + strDate
            break
        default:
            currentDate = date.getFullYear() + '-' + month + '-' + strDate +
                ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()

    }
    return currentDate
}

Vue.prototype.formatDate = function(val, status) {
    return Vue.prototype.getNowTime(val, status)
}


// 获取当前时间
Vue.prototype.getNowDate = function(val, type) {
    let date = val ? new Date(val) : new Date()
    let month = date.getMonth() + 1
    let strDate = date.getDate()
    // console.log(date);
    if (month >= 1 && month <= 9) {
        month = '0' + month
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate
    }
    let currentDate = ''
    switch (type) {
        case 'Y':
            currentDate = date.getFullYear()
            break
        case 'M':
            currentDate = date.getFullYear() + '-' + month
            break
        case 'D':
            currentDate = date.getFullYear() + '-' + month + '-' + strDate
            break
        default:
            currentDate = date.getFullYear() + '-' + month + '-' + strDate
    }
    return currentDate
}

Vue.prototype.formatDate = function(val, status) {
    return Vue.prototype.getNowDate(val, status)
}

// val:验证的内容
// col:验证的字段
// type：验证的类型（如：require：必填，电话号码：phone）
Vue.prototype.customValidationErr = {}
let rules = {}
Vue.prototype.customValidation = function(val, prop) {
    if (rules[prop].rule) {
        let result = executeRule(val, rules[prop].rule, prop, rules[prop].message)
        if (result) {
            delArr(prop)
        }
        return result
    } else {
        let returnRes = true
        for (let i = 0; i < rules[prop].length; i++) {
            let item = rules[prop][i]
            let result = executeRule(val, item.rule, prop, item.message)
            //如果其中一个验证失败了，会返回false。然后不用再赋值了
            if (returnRes) {
                returnRes = result
            }
            if (!result) {
                break
            } else if (result) {
                delArr(prop)
            }
        }
        return returnRes
    }
}

function delArr(prop) {
    delete Vue.prototype.customValidationErr[prop]
}

function executeRule(val, rule, prop, message) {
    if (rule === 'required') {
        if (val === '' || val === undefined || val === null) {
            setErrorMag(prop, message)
            Vue.prototype.$message.error(message)
            return false
        } else {
            return true
        }
    }
    if (rule === 'number') {
        if (!(/^[+]{0,1}(\d+)$/.test(val))) {
            setErrorMag(prop, message)
            Vue.prototype.$message.error(message)
            return false
        } else {
            return true
        }
    }
    if (rule === 'decimal') {
        if (!(/^[0-9]+([.]{1}[0-9]+){0,1}$/.test(val))) {//不包括负数
            // if(!(/^(-){0,1}[0-9]+([.]{1}[0-9]+){0,1}$/.test(val))){//匹配负数
            setErrorMag(prop, message)
            Vue.prototype.$message.error(message)
            return false
        } else {
            return true
        }
    }
    if (rule === 'phone') {
        if (!(/^1[3456789]\d{9}$/.test(val))) {
            setErrorMag(prop, message)
            Vue.prototype.$message.error(message)
            return false
        } else {
            return true
        }
    }
}

function setErrorMag(prop, message) {
    Vue.prototype.customValidationErr[prop] = { message: message, prop: prop }
}

Vue.prototype.initErrMessage = function(val) {
    rules = val
    Vue.prototype.customValidationErr = {}
    // console.log(rules);
    for (let key in rules) {
        let item = rules[key]
        if (item.rule) {
            Vue.prototype.customValidationErr[key] = { message: item.message, prop: key }
        } else {
            Vue.prototype.customValidationErr[key] = { message: item[0].message, prop: key }
        }
    }
}

Vue.prototype.getLabelData = function(arrys) {
    let arr = []
    arrys.forEach(function(obj, i) {
        let item = {}
        if (obj) {
            item.value = obj.code
            item.label = obj.name
        }
        arr.push(item)
    })
    return arr
}


Vue.prototype.getBranchs = function(arrys) {
    let arr = []
    arrys.forEach(function(obj, i) {
        let item = {}
        if (obj) {
            item.value = obj.orgId
            item.label = obj.orgName
        }
        arr.push(item)
    })
    return arr
}

Vue.prototype.getChangeDatatTypes = function(arrys) {
    let arr = []
    arrys.forEach(function(obj, i) {
        let item = {}
        if (obj) {
            item.value = obj.id
            item.label = obj.name
        }
        arr.push(item)
    })
    return arr
}

Vue.prototype.getSelectTimes = function(times) {
    var date = new Date(times)
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    var d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    const time = y + '-' + m + '-' + d
    return time
}


//大写为常量
Vue.prototype.STATUS = [
    { label: '待提交审核', value: 'DRAFT', enabled: true },
    { label: '审核中', value: 'SUBMITTED', enabled: true },
    { label: '审核通过', value: 'PAST', enabled: true },
    { label: '审核不通过', value: 'REJECTED', enabled: true },
    // {label:'已作废',value:'DELETE',enabled:true}
]

Vue.prototype.status = [
    { label: '草稿中', value: 'DRAFT', enabled: true },
    { label: '审批中', value: 'SUBMITTED', enabled: true },
    { label: '审批通过', value: 'PAST', enabled: true },
    { label: '驳回', value: 'REJECTED', enabled: true }
    // {label:'已作废',value:'DELETE',enabled:true}
]
Vue.prototype.status2 = [
    { label: '待提交审核', value: 'DRAFT', enabled: true },
    { label: '审核中', value: 'SUBMITTED', enabled: true },
    { label: '审核通过', value: 'PAST', enabled: true },
    { label: '审核不通过', value: 'REJECTED', enabled: true }
    // {label:'已作废',value:'DELETE',enabled:true}
]

Vue.prototype.STATUS_WIDTH = 120//状态栏宽度
